package com.dd.test;

import com.dd.reggie.ReggieApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import javax.annotation.Resources;
import java.lang.reflect.Array;
import java.util.*;

//主配置类不在默认的包路径下，或者Spring Boot的自动扫描机制没有能够识别到它，可以通过`@SpringBootTest`的`classes`属性手动指定它。
@SpringBootTest(classes = ReggieApplication.class)
@RunWith(SpringRunner.class)
public class SpringDataRedis {
    @Resource
    private RedisTemplate redisTemplate;

    @Test
    public void testString(){
        redisTemplate.opsForValue().set("city", "hrb");
        Object city = redisTemplate.opsForValue().get("city");
        System.out.println(city);
    }

    @Test
    public void testHash(){
        HashOperations ops = redisTemplate.opsForHash();

        //写入一个值
        ops.put("001", "name", "xiaoming");

        //写入多个值
        HashMap<String, String> map = new HashMap<>();
        map.put("age", "15");
        map.put("address", "hrb");
        ops.putAll("001", map);

        //获取单个值
        String age = ops.get("001", "age").toString();
        System.out.println(age);

        //获取全部键值对
        Map entries = ops.entries("001");
        System.out.println(entries.toString());

        //获取全部key
        Set keys = ops.keys("001");
        for(Object key : keys){
            System.out.println(key);
        }

        //获取全部value
        List values = ops.values("001");
        for(Object val : values){
            System.out.println(val);
        }

        //删除元素
        ops.delete("001", "age", "address");
        Map entries1 = ops.entries("001");
        System.out.println(entries1.toString());
    }

    //List列表可以类似于双向队列
    @Test
    public void testList(){
        ListOperations ops = redisTemplate.opsForList();

        //左边加入（左右均可）
        ops.leftPush("list1", "a");
        ops.leftPushAll("list1", "b", "c");

        //获取所有值，list长度
        Long size = ops.size("list1");
        List<String> list = ops.range("list1", 0, -1);
        for (String val : list) {
            System.out.println(val);
        }

        //右侧出队
        Object val1 = ops.rightPop("list1");
        System.out.println(val1);
    }

    @Test
    public void testSet(){
        SetOperations ops = redisTemplate.opsForSet();

        //添加集合元素
        ops.add("set1", "a", "b", "c", "t", "q");
        ops.add("set2", "b", "c", "d");

        //读取集合所有元素
        Set set1 = ops.members("set1");
        for(Object val : set1){
            System.out.println(val);
        }

        //移除元素
        ops.remove("set1", "t", "q");
        Set set = ops.members("set1");
        System.out.println(set.toArray());

        //取交集
        Set intersect = ops.intersect("set1", "set2");
        System.out.println("diff: " + Arrays.toString(intersect.toArray()));

        //取差集
        Set difference = ops.difference("set1", "set2");
        System.out.println("diff: " + Arrays.toString(difference.toArray()));

        //取并集
        Set union = ops.union("set1", "set2");
        System.out.println(Arrays.toString(union.toArray()));
    }

    @Test
    public void testZSet(){
        ZSetOperations ops = redisTemplate.opsForZSet();

        //1存值
        ops.add("zset", "a", 1.0);
        ops.add("zset", "b", 2.0);
        ops.add("zset", "c", 3.0);

        //取值
        Set<String> zset = ops.range("zset", 0, -1);
        System.out.println(zset.toArray());

        //按分数取值
        Set<String> rangedByScore = ops.rangeByScore("zset", 1.0, 2.0);
        System.out.println(rangedByScore.toArray());

        //增加元素分数
        ops.incrementScore("zset", "c", -1.5);
        Double score = ops.score("zset", "c");
        System.out.println("c: " + score);

        //移除元素
        ops.remove("zset", "a", "b");
        Set zset1 = ops.range("zset", 0, -1);
        System.out.println(zset1.toArray());
    }

    @Test
    public void testCommon(){
        //获取所有key
        Set keys = redisTemplate.keys("*");
        System.out.println(keys.toString());

        //判断某个key是否存在
        Boolean b = redisTemplate.hasKey("set2");
        System.out.println(b);

        //删除指定key
        Boolean deleteSet2 = redisTemplate.delete("set2");
        System.out.println(deleteSet2);

        //获取指定key对应value的数据类型
        DataType dataType = redisTemplate.type("set1");
        System.out.println(dataType.toString());

    }
}
